package com.warm.jpa.controller;

import com.warm.jpa.entity.BaseUserEntity;
import com.warm.jpa.repository.BaseUserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Optional;

@Controller
@Api(value = "Thymeleaf Test")
@RequestMapping(value = "/model")
public class ThymeleafController {

    @Autowired
    private BaseUserRepository baseUserRepository;

    /**
     *  public class ExtendedModelMap extends ModelMap implements Model
     *
     *  Model only use data transfer, not do business addressing.
     *  ModelAndView can do business addressing, need manual new Object --> new ModelAndView()
     */

    @ApiOperation("列表页面表单填充数据")
    @GetMapping(value = "/list")
    public String list(Model model){
        List<BaseUserEntity> list = baseUserRepository.findAll();
        model.addAttribute("list", list);
        return "demo-list";
    }

    @ApiOperation("查看详细信息")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public String info(@PathVariable("id") String id, Model model){
        Optional<BaseUserEntity> entity = baseUserRepository.findById(id);
        if(entity.isPresent()){
            model.addAttribute("entity", entity.get());
        }
        return "demo-info";
    }

    @ApiOperation("新增表单填充空数据对象")
    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String form(Model model){
        model.addAttribute("entity", new BaseUserEntity());
        return "demo-form";
    }

    @ApiOperation("编辑时为表单填充数据")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable("id") String id, Model model){
        Optional<BaseUserEntity> entity = baseUserRepository.findById(id);
        if(entity.isPresent()){
            model.addAttribute("entity", entity.get());
        }
        return "demo-form";
    }

    @ApiOperation("保存实例用户")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(BaseUserEntity entity){
        if(entity.getId() != null && !"".equals(entity.getId())){
            baseUserRepository.updateById1(entity.getName(), entity.getId());
        }else {
            baseUserRepository.save(entity);
        }
        return "index";
    }

    @ApiOperation("删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id){
        baseUserRepository.deleteById(id);

        return "/list";
    }

    @ApiOperation("Result ModelMap")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(ModelMap model){
        List<BaseUserEntity> list = baseUserRepository.findAll();
        model.addAttribute("userList", list);
        return "index";
    }

    @ApiOperation("Result ModelAndView")
    @RequestMapping(value = "/test2", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView test2(){
        List<BaseUserEntity> list = baseUserRepository.findAll();
        //ModelAndView model = new ModelAndView("index"); // Set business address : equals model.setViewName("index")
        ModelAndView model = new ModelAndView();
        model.addObject("list", list);
        model.setViewName("index");
        return model;
    }
}
